//
//  TPPriceDetialController.m
//  TravelProject
//
//  Created by lanou3g on 15/11/26.
//  Copyright © 2015年 韩婷婷. All rights reserved.
//

#import "TPPriceDetialController.h"
#import "TPPriceDetailCell.h"
#import "HT_DataDownloadTools.h"
#import "MJRefresh.h"
#import "PriceModel.h"

@interface TPPriceDetialController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger page;

@end

@implementation TPPriceDetialController
+(instancetype)sharePriceVC{
    static TPPriceDetialController *price = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        price = [mainStoryBoard instantiateViewControllerWithIdentifier:@"price"];
    });
    return price;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setupCollectionView];
    [self addHeader];
    [self addFooter];

    // Do any additional setup after loading the view.
}
//懒加载
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)setupCollectionView{
    self.layout.itemSize = CGSizeMake((CGRectGetWidth([UIScreen mainScreen].bounds) - 30)/2, 180);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:@"TPPriceDetailCell" bundle:[NSBundle mainBundle]];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"priceCell"];
}
- (void)solveData:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *dict = dic[@"data"];
    NSArray *array = dict[@"lastminutes"];
    for (NSDictionary *d in array) {
        PriceModel *model = [[PriceModel alloc]init];
        [model setValuesForKeysWithDictionary:d];
        [self.dataArray addObject:model];
    }
}

//上啦加载,下拉刷新
- (void)addHeader{
    __unsafe_unretained typeof (self) vc = self;
    [self.collectionView addHeaderWithCallback:^{
        self.page = 1;
        NSString *stringURL = [NSString stringWithFormat:@"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=865308021990677&track_app_version=6.8.2&track_app_channel=hicloud&track_device_info=hwH30-T00&track_os=Android4.2.2&app_installtime=1448007324702&lat=40.030276&lon=116.343785&page_size=10&page=%ld&product_type=0&continent_id=0&country_id=0&city_id=0",self.page];
        [HT_DataDownloadTools downloadDataWithURL:@"http://open.qyer.com/lastminute/app_lastminute_list" andMethod:@"POST" andBody:stringURL andBlock:^(NSData *data) {
            [self solveData:data];
            [vc.collectionView reloadData];
            [vc.collectionView headerEndRefreshing];
        }];
    }];
    [self.collectionView headerBeginRefreshing];
}
- (void)addFooter{
    __unsafe_unretained typeof(self) vc = self;
    [self.collectionView addFooterWithCallback:^{
        if (self.page < 5) {
            self.page ++;
            NSString *stringURL = [NSString stringWithFormat:@"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=865308021990677&track_app_version=6.8.2&track_app_channel=hicloud&track_device_info=hwH30-T00&track_os=Android4.2.2&app_installtime=1448007324702&lat=40.030276&lon=116.343785&page_size=10&page=%ld&product_type=0&continent_id=0&country_id=0&city_id=0",self.page];
            [HT_DataDownloadTools downloadDataWithURL:@"http://open.qyer.com/lastminute/app_lastminute_list" andMethod:@"POST" andBody:stringURL andBlock:^(NSData *data)  {
                [self solveData:data];
                [vc.collectionView reloadData];
                [vc.collectionView footerEndRefreshing];
            }];
        }else{
             [vc.collectionView footerEndRefreshing];
        }
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TPPriceDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"priceCell" forIndexPath:indexPath];
    PriceModel *model =  self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
